package com.yumingjiang.filter;

import jakarta.servlet.Filter;
import jakarta.servlet.FilterChain;
import jakarta.servlet.ServletException;
import jakarta.servlet.ServletRequest;
import jakarta.servlet.ServletResponse;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.MDC;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.util.UUID;

/**
 * @description:
 * @author: John
 * @version: URIFilter.java, v1.0 2025年01月01日 00:32
 */
@Slf4j
@Component
public class TraceIdFilter implements Filter {

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        MDC.put("traceId", UUID.randomUUID().toString().replace("-", ""));
        log.debug(">>>>>> TraceIdFilter traceId put. <<<<<<<<");
        filterChain.doFilter(servletRequest, servletResponse);
        MDC.clear();
        log.debug(">>>>>> TraceIdFilter traceId clear. <<<<<<<<");
    }
}